package com.tongji.showroom.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	public class Blending extends Sprite
	{
		
		public function Blending()
		{
			
		}
		
		//返回色彩平衡后的目标图片Bitmap的BitmapData
		public static function blending(backbitmap:BitmapData, frontbitmap:BitmapData, x:int, y:int):BitmapData
		{
			var sv:Array = getBackSV(backbitmap, frontbitmap, x, y);
			for (var j:int = 0; j < frontbitmap.height; j++) 
			{
				for (var k:int = 0; k < frontbitmap.width; k++) 
				{
					var c:uint = frontbitmap.getPixel32(k, j);
					if((c >> 24 & 0xFF) != 0)
					{
						var color_new:uint = (0xFF << 24) + change(sv[0], sv[1], c);
						frontbitmap.setPixel32(k, j, color_new);
					}
				} 
			}
			return frontbitmap;
		}
		
		//获得背景图片绕前景图片一周的像素的平均RGB值，并将其转换成HSV
		public static function getBackSV(backbitmap:BitmapData, frontbitmap:BitmapData, x:int, y:int):Array
		{
			var sv:Array = new Array(2);
			var col:Array = new Array();
			var sum_r:uint = 0;
			var sum_g:uint = 0;
			var sum_b:uint = 0;
			
			var color_back:uint = 0;
			var count:int = 0;
			
			col.push(backbitmap.getPixel(x - 1, y - 1));
			col.push(backbitmap.getPixel(x + frontbitmap.width / 2, y - 1));
			col.push(backbitmap.getPixel(x + frontbitmap.width + 1, y - 1));
			col.push(backbitmap.getPixel(x - 1, frontbitmap.height / 2));
			col.push(backbitmap.getPixel(x + frontbitmap.width + 1, frontbitmap.height / 2));
			col.push(backbitmap.getPixel(x - 1, y + frontbitmap.height + 1));
			col.push(backbitmap.getPixel(x + frontbitmap.width / 2, y + frontbitmap.height + 1));
			col.push(backbitmap.getPixel(x + frontbitmap.width + 1, y + frontbitmap.height + 1));
			
			for (var i:int = 0; i < col.length; i++) 
			{
				sum_r = sum_r + (col[i] >> 16 & 0xFF);
				sum_g = sum_g + (col[i] >> 8 & 0xFF);
				sum_b = sum_b + (col[i] & 0xFF);
			}
			
			var r:uint = sum_r / col.length;
			var g:uint = sum_g / col.length;
			var b:uint = sum_b / col.length;
			
			var back_r:Number = r / 255;
			var back_g:Number = g / 255;
			var back_b:Number = b / 255;
			
			var max_b:Number = max(back_r, back_g, back_b);
			var min_b:Number = min(back_r, back_g, back_b);
			
			var s_b:Number;
			var v_b:Number = max_b;
			
			if(max_b == min_b)//RGB --> HSV		求背景的HSV中的SV
			{
				s_b = 0;
			}
			else
			{
				s_b = (max_b - min_b) / max_b;
			}
			
			sv[0] = s_b;
			sv[1] = v_b;
			
			return sv;
		}
		
		//计算前景图片新的RGB值
		public static function change(s_b:Number, v_b:Number, color_up:uint):uint
		{
			var up_r:Number = (color_up >> 16 & 0xFF) / 255;
			var up_g:Number = (color_up >> 8 & 0xFF) / 255;
			var up_b:Number = (color_up & 0xFF) / 255;
			
			var max_u:Number = max(up_r, up_g, up_b);
			var min_u:Number = min(up_r, up_g, up_b);
			
			var h_u:Number;
			var s_u:Number;
			var v_u:Number = max_u;
			
			var h:Number;
			var s:Number;
			var v:Number;
			
			var r:uint;
			var g:uint;
			var b:uint;
			
			if(max_u == min_u)//RGB --> HSV		求物体的HSV
			{
				h_u = 0;
				s_u = 0;
			}
			else
			{
				s_u = (max_u - min_u) / max_u;
				var del_r_u:Number = (((max_u - up_r) / 6) + (max_u - min_u) / 2) / (max_u - min_u);
				var del_g_u:Number = (((max_u - up_g) / 6) + (max_u - min_u) / 2) / (max_u - min_u);
				var del_b_u:Number = (((max_u - up_b) / 6) + (max_u - min_u) / 2) / (max_u - min_u);
				if(up_r == max_u)
					h_u = del_b_u - del_g_u;
				else if(up_g == max_u)
					h_u = (1 / 3) + del_r_u - del_b_u;
				else if(up_b == max_u)
					h_u = (2 / 3) + del_g_u - del_r_u;
				if(h_u < 0)
					h_u = h_u + 1;
				if(h_u > 1)
					h_u = h_u - 1;
			}
			
			h = h_u;
			s = s_u * 0.60 + s_b * 0.40;
			v = v_u * 0.60 + v_b * 0.40;
			
			if(s == 0)//HSV --> RGB
			{
				r = v * 255;
				g = v * 255;
				b = v * 255;
			}
			else
			{
				var var_h:Number = h * 6;
				if(var_h == 6)
					var_h = 0;
				var var_i:int = int(var_h);
				var var_1:Number = v * (1 - s);
				var var_2:Number = v * (1 - s * (var_h - var_i));
				var var_3:Number = v * (1 - s * (1 - (var_h - var_i)));
				
				var var_r:Number;
				var var_g:Number;
				var var_b:Number;
				
				if(var_i == 0)
				{
					var_r = v;
					var_g = var_3;
					var_b = var_1;
				}
				else if(var_i == 1)
				{
					var_r = var_2;
					var_g = v;
					var_b = var_1;
				}
				else if(var_i == 2)
				{
					var_r = var_1;
					var_g = v;
					var_b = var_3;
				}
				else if(var_i == 3)
				{
					var_r = var_1;
					var_g = var_2;
					var_b = v;
				}
				else if(var_i == 4)
				{
					var_r = var_3;
					var_g = var_1;
					var_b = v;
				}
				else
				{
					var_r = v;
					var_g = var_1;
					var_b = var_2;
				}
				
				r = var_r * 255;
				g = var_g * 255;
				b = var_b * 255;
			}
			
			var color:uint = (r << 16) + (g << 8) + b;
			return color;
		}
		
		public static function max(r:Number, g:Number, b:Number):Number
		{
			var max1:Number = r > g ? r : g;
			var max2:Number = max1 > b ? max1 : b;
			return max2;
		}
		
		public static function min(r:Number, g:Number, b:Number):Number
		{
			var min1:Number = r < g ? r : g;
			var min2:Number = min1 < b ? min1 : b;
			return min2;
		}
	}
}